/**
 * Created by zqk on 2018/4/16.
 */
$(document).ready(function () {

    var form = new xhForm($('#xh_form'));
    form.parse();
    $(".form-date").datetimepicker({format: 'yyyy-mm-dd',startDate:'2010-01-01',language: 'zh-CN',minView: "month", autoclose:true});
    var iProjectGrade=addressUri.getParam('iProjectGrade');
    var iParentPid=addressUri.getParam('iParentPid');
    var iProjectId=addressUri.getParam('iProjectId');

    //获取可添加的员工
    $.ajax({
        url:moduleUrl+'project/getProjectMemberList?iProjectGrade='+iProjectGrade+'&iParentPid='+iParentPid,
        type:'POST',
        dataType:'json',
        // async :false,//同步才能赋值
        success:function (data) {
            userIds = data;
            var appPackDom = $('#memberIds');
            if(userIds.length > 0){
                var appArr = [];
                $.each(userIds, function(id, value){
                    appArr.push({id: value['iUserId'], text: value['sStaffName']});
                });
                appPackDom.select2({
                    placeholder: '选择项目成员',
                    width: "100%",
                    data: appArr,
                });
            }

            //获取默认的成员选项
            $.ajax({
                url:moduleUrl+'project/getCheckedMemberIds',
                data:{
                    iParentPid:iParentPid,
                    iProjectGrade:iProjectGrade,
                    iProjectId:iProjectId,
                },
                type:'POST',
                dataType:'json',
                success:function (data) {
                    $('#memberIds').val(data).trigger('change');
                    // $("#memberIds").val(data);
                    // $("#memberIds").select2();
                },
                error:function (data) {
                    console.log(data);
                }
            });
        },
        error:function (data) {
            console.log(data);
        }
    });


    //获取应用列表
    $.ajax({
        url:moduleUrl+'project/getProducts',
        type:'POST',
        dataType:'json',
        success:function (data) {
            var apps = data;
            var appPackDom = $('#apptag');
            if(apps.length > 0){
                var appArr = [];
                appArr.push({id: 0, text: '请选择'});
                $.each(apps, function(id, value){
                    appArr.push({id: value['id'], text: value['text']});
                });
                appPackDom.select2({
                    placeholder: '选择产品',
                    width: "100%",
                    data: appArr,
                });
            }

            //编辑项目
            if(iProjectId!=null){ //edit
                $.ajax({
                    url:moduleUrl+'project/getProjectInfo',
                    data:{id:iProjectId},
                    type:'POST',
                    dataType:'json',
                    success:function (data) {
                        form.fill(data);
                        if(data.iProductId){
                            $("#apptag").val(data.iProductId).trigger('change');
                        }
                        form.bindRequired($('#bt_modal_confirm',parent.document));
                    },
                    error:function (data) {
                        window.top.xhTop.showError(
                            data.responseJSON.error,
                            data.responseJSON.msg
                        );
                    }
                });
            } else { //create  创建项目
                if(iParentPid!=null){
                    $.ajax({
                        url:moduleUrl+'project/getProjectInfo',
                        data:{id:iParentPid},
                        type:'POST',
                        dataType:'json',
                        success:function (data) {
                            $("#apptag").val(data.iProductId).trigger('change')
                            // $("#apptag").val(data.iProductId);
                            // $("#apptag").select2();
                        },
                        error:function (data) {
                            window.top.xhTop.showError(
                                data.responseJSON.error,
                                data.responseJSON.msg
                            );
                        }
                    });
                }
                form.bindRequired($('#bt_modal_confirm',parent.document));
            }

        },
        error:function (data) {
            console.log(data);
        }
    });

    //表单提交
    $('#bt_modal_confirm',parent.document).click(function () {
        $("#apptag").removeAttr("disabled");
        xhExtra.setBtnLoading($('#bt_modal_confirm',parent.document));
        $('#bt_modal_confirm',parent.document).addClass('disabled');
        $.ajax({
            url:moduleUrl+'project/saveProject',
            data:$('#xh_form').serialize(),
            type:'POST',
            success:function (data) {
                window.top.xhTop.showSuccess(
                    data.msg
                );
                window.top.xhTop.getActiveFrame().projectList.treegrid('reload');
                window.top.xhTop.closeModal();
            },
            error:function (data) {
                xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                $('#bt_modal_confirm',parent.document).removeClass('disabled');
                window.top.xhTop.showError(
                    data.responseJSON.error,
                    data.responseJSON.msg
                );
            }
        });

    });

    // modalBind.bindClick();

    

});